import 'dart:io';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'debug_utils.dart';

class FileUtils {
  static Future<String> compressImage(File file) async {
    Debug.info("压缩前------>${file.path}");
    int index = file.path.lastIndexOf("/");
    String fileDir = file.path.substring(0, index);
    String fileName = file.path.substring(index + 1);
    String compressFilePath = "$fileDir/compress_$fileName";
    File? compressFile = await FlutterImageCompress.compressAndGetFile(
      file.absolute.path,
      compressFilePath,
      quality: 50,
      rotate: 0,
    );
    Debug.info("压缩后------>$compressFilePath");
    if (compressFile != null) {
      // 压缩成功
      Debug.info("压缩成功");
      return compressFilePath;
    } else {
      // 压缩失败 上传原图
      Debug.info("压缩失败");
      return file.path;
    }
  }
}
